package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.qyxx.entity.*;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgFpTopSygysMapper;
import org.jeecg.modules.qyxx.service.IBgFpTopSygysService;
import org.jeecg.modules.qyxx.service.IBgFpTopSygysTqService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 上游前10供应商数据
 * @Author: ZJJ
 * @Date: 2021-06-09
 * @Version: V1.0
 */
@DS("fk_oracle1")
@Service
public class BgFpTopSygysServiceImpl extends MppServiceImpl<BgFpTopSygysMapper, BgFpTopSygys> implements IBgFpTopSygysService {
    @Autowired
    private BgFpTopSygysMapper bgFpTopSygysMapper;
    @Autowired
    private IBgFpTopSygysTqService bgFpTopSygysTqService;
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;

    @Override
    public List fpTopSygys(String djxh, List<String> tjnds,Date jssj) {
        DateUtils utils = DateUtils.getInstance();
        String year = utils.getYearToStr(jssj);
        // String year = bgSjmkjgOrderService.getNewByDjxh(djxh);
        // 获取4年的数据
        List<String> list = new ArrayList<>();
        Integer t = Integer.valueOf(tjnds.get(0)) - 1;
        list.add(t.toString());
        list.addAll(tjnds);
        List<Object> list1 = new ArrayList<>();
        List<BgFpTopSygys> oldBgFpTopSygys = null;
        for (String tjnd : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            BigDecimal fpsl = BigDecimal.ZERO;// 发票数量合计
            BigDecimal fpje = BigDecimal.ZERO;// 发票金额合计
            BigDecimal jezb = BigDecimal.ZERO;// 发票占比合计
            if (tjnd.equals(DateUtils.getInstance().getYearToStr(new Date()))) {
                LambdaQueryWrapper<BgFpTopSygysTq> wrapper = Wrappers.lambdaQuery(BgFpTopSygysTq.class);
                wrapper.eq(BgFpTopSygysTq::getDjxh, djxh).eq(BgFpTopSygysTq::getTjnd, tjnd);
                List<BgFpTopSygysTq> lastYearTqList = bgFpTopSygysTqService.list(wrapper);

                List<BgFpTopSygys> bgFpTopSygysList = baseMapper.selectBgFpTopSygysMaps(djxh, tjnd);

                for (BgFpTopSygys bgFpTopSygys : bgFpTopSygysList) {
                    bgFpTopSygys.setFpje(bgFpTopSygys.getFpje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    for (int i = 0; i < lastYearTqList.size(); i++) {
                        BgFpTopSygysTq sygys = lastYearTqList.get(i);
                        if (bgFpTopSygys.getXsfNsrsbh().equals(sygys.getXsfNsrsbh()) && bgFpTopSygys.getXsfmc().equals(sygys.getXsfmc())) {
                            bgFpTopSygys.setChange("维持");
                            break;
                        } else if (i == lastYearTqList.size() - 1) {
                            bgFpTopSygys.setChange("新增");
                        }
                    }
                    fpsl = fpsl.add(bgFpTopSygys.getFpsl());
                    fpje = fpje.add(bgFpTopSygys.getFpje());
                    jezb = jezb.add(bgFpTopSygys.getJezb());
                }
                BgFpTopSygys bgFpTopSygys = new BgFpTopSygys();
                bgFpTopSygys.setJezb(jezb);
                bgFpTopSygys.setFpje(fpje);
                bgFpTopSygys.setFpsl(fpsl);
                bgFpTopSygys.setXsfmc("合计");
                bgFpTopSygysList.add(bgFpTopSygys);
                if (year.equals(tjnd)) {
                    // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                    String month = utils.getMonthToStr(jssj);
                    String newMonth = month.substring(month.length() - 2);
                    map.put("tjny",year + "年(" + newMonth + "月)");
                    map.put("dataList", bgFpTopSygysList);
                } else {
                    map.put("tjny",tjnd + "年");
                    map.put("dataList",  bgFpTopSygysList);
                }
                list1.add(map);
                // map.put(tjnd, bgFpTopSygysList);
            } else {
                List<BgFpTopSygys> bgFpTopSygysList = bgFpTopSygysMapper.selectBgFpTopSygysMaps(djxh, tjnd);
                if (oldBgFpTopSygys != null && oldBgFpTopSygys.size() > 0) {
                    for (BgFpTopSygys bgFpTopSygys : bgFpTopSygysList) {
                        for (int i = 0; i < oldBgFpTopSygys.size(); i++) {
                            BgFpTopSygys sygys = oldBgFpTopSygys.get(i);
                            if (bgFpTopSygys.getXsfNsrsbh().equals(sygys.getXsfNsrsbh()) && bgFpTopSygys.getXsfmc().equals(sygys.getXsfmc())) {
                                bgFpTopSygys.setChange("维持");
                                break;
                            } else if (i == oldBgFpTopSygys.size() - 1) {
                                bgFpTopSygys.setChange("新增");
                            }
                        }
                        /*if (year.equals(bgFpTopSygys.getTjnd())) {
                            String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                            String newMonth = month.substring(month.length() - 2);
                            bgFpTopSygys.setTjnd(year + "年(" + newMonth + "月)");
                        } else {
                            bgFpTopSygys.setTjnd(bgFpTopSygys.getTjnd() + "年");
                        }*/
                        fpsl = fpsl.add(bgFpTopSygys.getFpsl());
                        fpje = fpje.add(bgFpTopSygys.getFpje());
                        jezb = jezb.add(bgFpTopSygys.getJezb());
                    }
                    BgFpTopSygys bgFpTopSygys = new BgFpTopSygys();
                    bgFpTopSygys.setJezb(jezb);
                    bgFpTopSygys.setFpje(fpje);
                    bgFpTopSygys.setFpsl(fpsl);
                    bgFpTopSygys.setXsfmc("合计");
                    bgFpTopSygysList.add(bgFpTopSygys);
                    if (year.equals(tjnd)) {
                        // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                        String month = utils.getMonthToStr(jssj);
                        String newMonth = month.substring(month.length() - 2);
                        map.put("tjny",year + "年(" + newMonth + "月)");
                        map.put("dataList", bgFpTopSygysList);
                    } else {
                        map.put("tjny",tjnd + "年");
                        map.put("dataList",  bgFpTopSygysList);
                    }
                    list1.add(map);
                    // map.put(tjnd, bgFpTopSygysList);
                }
                for (BgFpTopSygys bgFpTopSygys : bgFpTopSygysList) {
                    if (bgFpTopSygys.getFpje() != null) {
                        bgFpTopSygys.setFpje(bgFpTopSygys.getFpje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                    }
                }
                oldBgFpTopSygys = bgFpTopSygysList;
            }
        }
        return list1;
    }
}
